package com.elite.www;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.kmpro.util.KmproSign;

public class callRedirect extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(callRedirect.class);

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String redirString = "";

        log.info("StartServlet接收调用报文信息：" + System.getProperty("line.separator") + request.getQueryString());

        ServletConfig config = getServletConfig();
        String jsonURL = config.getInitParameter("SWKM_URL");
        String jsonKey = null;
        String jsonLoginname = null;

        jsonURL = (jsonURL == null || jsonURL.length() <= 0) ? request.getParameter("url") : jsonURL;
        jsonKey = request.getParameter("key");
        jsonLoginname = request.getParameter("loginname");

        log.debug("StartServlet 解析调用报文(url):" + System.getProperty("line.separator") + jsonURL);
        log.debug("StartServlet 解析调用报文(key):" + System.getProperty("line.separator") + jsonKey);
        log.debug("StartServlet 解析调用报文(loginname):" + System.getProperty("line.separator") + jsonLoginname);

        String sign = KmproSign.sign(jsonLoginname, jsonKey);

        log.debug("StartServlet 生成签名(sign):" + sign);

        redirString = jsonURL + "?loginName=" + jsonLoginname + "&sign=" + sign + "";

        log.debug("StartServlet 返回重定向URL信息:" + System.getProperty("line.separator") + redirString);

        response.sendRedirect(redirString);
    }
}